package com.csr.csrcheck.service.impl;

import com.csr.csrcheck.mapper.ClinicMapper;
import com.csr.csrcheck.pojo.Clinic;
import com.csr.csrcheck.service.ClinicService;
import com.csr.csrcheck.service.ex.ClinicException;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.util.PageResult;
import com.csr.csrcheck.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClinicServiceImpl implements ClinicService {
    @Resource
    private ClinicMapper clinicMapper;

    public List<Clinic> getCliniclist() {
        List<Clinic> list = this.clinicMapper.getCliniclist();
        if (list == null)
            throw new ClinicException("");
        return list;
    }

    public PageResult getListpage(int pageNum, int pageSize, String product_name) {
        return PageUtils.getPageResult(pageNum, pageSize, getPageInfo(pageNum, pageSize, product_name));
    }

    public void addClinic(Clinic clinic) {
        int row = this.clinicMapper.addClinic(clinic);
        if (row != 1)
            throw new CompanyException("");
    }

    public int updateClinic(Clinic clinic) {
        int row = this.clinicMapper.updateClinic(clinic);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    public Clinic findclinicByid(int id) {
        Clinic list = this.clinicMapper.findclinicByid(id);
        if (list == null)
            throw new CompanyException("");
        return list;
    }

    public int deleteclinicByid(int id) {
        int row = this.clinicMapper.deleteclinicByid(id);
        if (row != 1)
            throw new CompanyException("");
        return row;
    }

    private PageInfo<Clinic> getPageInfo(int pageNum, int pageSize, String product_name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Clinic> list = this.clinicMapper.getCliniclistpage(product_name);
        if (list == null)
            throw new CompanyException("");
        return new PageInfo(list);
    }
}
